<?php
namespace app\api\controller;
use \think\Controller;
use \think\Db;
use \think\Session;
header("Access-Control-Allow-Origin:http://127.0.0.1:8020");
//这里只支持一个域名，所有前端测试都用统一域名，不要使用IP
header('Access-Control-Allow-Methods:POST'); // 响应类型post
header('Access-Control-Allow-Credentials:true'); //运行跨域操作
header('Access-Control-Allow-Headers:x-requested-with, content-type'); // 响应头设置
class carlist extends Controller
{
	public function index(){
		$id=input('id');
		if($id==null){
		$car=Db::name("carlist")->select();
		return json([ 'status'=> 0,
			          'massage'=>$car]);
	}else{
		$car=Db::name('carlist')->where('id',$id)->find();
		return json($car);
		
	}
	}
	////比赛列表和详情
	public function add(){
		$name=input('post.name');
       

        
        $reg=Db::name('carlist')->where('name',$name)->find();

        if($reg){
              return json([
                          'status'=>2,
                          'massage'=>'已经有了',
                              ]);
        }else if ($name==null) {
            return json([
              'status'=>1,
              'massage'=>'不能添加空的',
            ]);
           
          }else{
          $date=['name'=>$name];
          $car=Db::name('carlist')->insert($date);
          $cars=Db::name("carlist")->select();
           // $user =DB::name('users')->where('u_name',$uname)->find();
           if ($car) {
           	 return json(['status'=>0,'massage'=>$cars]);
           }
            
          }
    }

}